package com.bd.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

import com.bd.constants.ResourcesConstants;
import com.bd.controller.AppMainController;
import com.bd.model.Faixa;
import com.bd.util.StringUtil;
import com.jgoodies.forms.factories.CC;
import com.jgoodies.forms.layout.FormLayout;

public class CreationWindow extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long		serialVersionUID	= -3299477562207727240L;
	private Class<?>				genClass;
	private StringBuilder			columns				= new StringBuilder("3dlu, 100px:grow, 3dlu, 100px:grow, 3dlu");
	private StringBuilder			lines				= new StringBuilder("");
	private Map<String, JTextField>	fields				= new HashMap<String, JTextField>();
	private JButton					saveButton;
	private JButton					closeButton			= new JButton("Close");
	private int						countLines			= 2;
	private JPanel					contentPane			= new JPanel();

	/**
	 * Create the panel.
	 */
	public CreationWindow(Class<?> genClass) {
		setContentPane(contentPane);
		this.genClass = genClass;
		createLines();
		lines.append("3dlu, default:grow,");
		lines.append("3dlu, default, 3dlu");
		initComponents();
		setSize(new Dimension(300, 300));
		setLocationRelativeTo(null);
	}

	private void createLines() {
		Field[] fields = genClass.getDeclaredFields();
		StringBuilder builder = new StringBuilder();
		for (Field f : fields)
			if (!f.getType().isInstance(new ArrayList<>())) {
				String name = f.getName();
				char[] chrs = name.toCharArray();

				for (int i = 0; i < chrs.length; i++)
					if (i == 0)
						builder.append((chrs[0] + "").toUpperCase());
					else if (Character.isUpperCase(chrs[i]))
						builder.append(" " + chrs[i]);
					else
						builder.append(chrs[i]);

				JTextField field = new JTextField();
				field.setEditable(true);
				if (builder.toString().contains("Fk"))
					builder.replace(0, 2, "Codigo");
				this.fields.put(builder.toString(), field);
				builder = new StringBuilder();
				lines.append("3dlu, default,");
				countLines += 2;
			}
	}

	private void initComponents() {
		contentPane.setLayout(new FormLayout(columns.toString(), lines.toString()));
		createFields();
		saveButton = new JButton("Salvar " + StringUtil.formatClassName(genClass));
		saveButton.setIcon(ResourcesConstants.ICON_SAVE);
		closeButton.addActionListener(new CloseButtonListener());
		closeButton.setIcon(ResourcesConstants.ICON_CLOSE);
		countLines += 2;

		contentPane.add(saveButton, CC.xy(2, countLines, CC.FILL, CC.FILL));
		contentPane.add(closeButton, CC.xy(4, countLines, CC.FILL, CC.FILL));
		// setBorder(new LineBorder(Color.BLACK));
		((JPanel) getContentPane()).setBorder(BorderFactory.createCompoundBorder(new LineBorder(Color.GRAY.brighter()), new LineBorder(Color.GRAY)));
	}

	private void createFields() {
		int i = 4;
		AppMainController ctrl = AppMainController.getInstance();
		for (Entry<String, JTextField> entry : fields.entrySet()) {
			add(new JLabel(entry.getKey()), CC.xy(2, i/* , CC.RIGHT, CC.FILL) */));
			if (entry.getKey().contains("Id")) {
				int lastId = ctrl.findLastId(StringUtil.formatClassName(genClass));
				entry.getValue().setText(lastId + 1 + "");
				entry.getValue().setEnabled(false);
			}
			contentPane.add(entry.getValue(), CC.xy(4, i));
			i += 2;
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}

	class CloseButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			dispose();
		}
	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			// TODO: handle exception
		}
		final JFrame frm = new CreationWindow(Faixa.class);
		frm.setVisible(true);
		frm.setEnabled(true);
		// frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
